package com.docMan.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.docMan.dao.BaseDao;
import com.docMan.dao.TchumanDao;
import com.docMan.dao.TchumanroleDao;
import com.docMan.dao.pojo.DocAdmin;
import com.docMan.dao.pojo.Tchuman;
import com.docMan.dao.pojo.Tcrole;
import com.docMan.service.DocAdminService;
import com.docMan.service.TcroleService;
import com.docMan.utils.FinalConfig;
import com.docMan.utils.ProjectUtils;

@Service("tcroleService")
public class TcroleServiceImpl extends BaseServiceImpl<Tcrole> implements TcroleService{
	
	private TchumanroleDao tchumanroleDao;
	private HttpSession session;
	private TchumanDao tchumanDao;
	private DocAdminService docAdminService;
	


	@Resource
	public void setDocAdminService(DocAdminService docAdminService) {
		this.docAdminService = docAdminService;
	}

	@Resource(name="tcroleDao")
	@Override
	public void setDao(BaseDao<Tcrole> dao) {
		// TODO Auto-generated method stub
		super.setDao(dao);
	}





	@Resource(name="tchumanDao")
	public void setTchumanDao(TchumanDao tchumanDao) {
		this.tchumanDao = tchumanDao;
	}





	@Autowired
	public void setSession(HttpSession session) {
		this.session = session;
	}



	public void setTchumanroleDao(TchumanroleDao tchumanroleDao) {
		this.tchumanroleDao = tchumanroleDao;
	}



	@Override
	public String saveUpdate(int roleid,int unitid, String rolename, String roledescr) {
		Tcrole tcrole=new Tcrole();
		tcrole.setRolename(rolename);
		tcrole.setRoledescription(roledescr);
		tcrole.setUnitid(1376);
		tcrole.setRoleproperty(0);
		if(roleid!=0){
			tcrole.setRoleid(roleid);
		}
		try {
			saveOrUpdate(tcrole);
			clear();
			put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			put("success", false);
			put("message", "�������");
		}
		
		return getMap();
	}
	
	

	@Override
	@Transactional
	public String deleteRole(int roleid) {
		try {
			delete(roleid);
			tchumanroleDao.update("delete from Tchumanrole hr where hr.roleid=?",roleid);
			put("success", true);
		} catch (Exception e) {
			put("success", false);
			put("message", "ɾ�����");
		}
		
		return getMap();
	}



	@SuppressWarnings("unchecked")
	@Override
	public String getRole(final int humanId) {
		try {
			String sqlWjQxString="select DA_WJQX.QXCODE FROM DA_WJQX WHERE DA_WJQX.ID IN (select DA_QX_WJ.WJID from DA_QX_WJ where DA_QX_WJ.QXID in (select DA_QX_ROLE.QXID FROM DA_QX_ROLE WHERE DA_QX_ROLE.ROLEID IN ( ";
			String  roleString="SELECT DLSYS.TCHUMANROLE.ROLEID FROM DLSYS.TCHUMANROLE WHERE DLSYS.TCHUMANROLE.HUMANID=?";
			List<Integer> roleids=sqlQueryNoCache(roleString, humanId);
			String roleidString="";
			for (int i=0;i<roleids.size();i++) {
				if(i==roleids.size()-1){
					roleidString+=roleids.get(i)+")))";
				}else{
					roleidString+=roleids.get(i)+",";
				}
			}
			session.setAttribute(FinalConfig.SESSION_ROLEIDS, roleids);
			
			String hql = "select h from Tchuman h where h.humanid=?";
			String keyString = ProjectUtils.getMemcachedKey(hql, humanId);
			List<Tchuman> list = tchumanDao.find(keyString, hql, humanId);
			if (list.size() > 0) {
				session.setAttribute(FinalConfig.SESSION_HUMANNAME, list.get(0).getHumanname());
				session.setAttribute(FinalConfig.SESSION_HUMANID, list.get(0).getHumanid());
				session.setAttribute(FinalConfig.SESSION_HUMAN_UNIT_ID, list.get(0).getUnitid());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}



	@SuppressWarnings("unchecked")
	@Override
	public String isAdmin() {
		String roleString ="select r from Tcrole r where r.rolename = ?";
		List<DocAdmin> docAdmins=docAdminService.findAll("");
		if(docAdmins.size()==0){
			session.setAttribute("admin", false);
			return null;
			
		}
		int humanId=(int)session.getAttribute(FinalConfig.SESSION_HUMANID);
		session.setAttribute("admin", false);
		for (DocAdmin docAdmin : docAdmins) {
			if(humanId==docAdmin.getHumanId().intValue()){
				session.setAttribute("admin", true);
			}
		}
		//session.setAttribute("admin", true);
		return null;
		
	}


	@Override
	public String getSessionValue(String key) {
		put("data", (boolean)session.getAttribute(key));
		return getMap();
	}
	
}
